package com.seeyon.ctp.rest.resources;

import com.seeyon.apps.ext.cop.button.service.impl.DataBtnServiceImpl;
import com.seeyon.apps.snail.button.service.ScriptBtnService;
import com.seeyon.ctp.rest.resources.BaseResourceExt;
import snail.common.utils.MapHelper;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.Map;

/**
 *
 * 创建者： ljy
 * 创建日期：2022年5月25日
 */
@Path("/cop/data_btn")
public class DataBtnController extends BaseResourceExt {
	
	public static final long V_TIMEOUT = 30000L;

	
	/**
	 * 
	 * @param data
	 * 参数：
	 * 		formId：表单定义ID
	 * 		btnId：自定义按钮ID
	 * 		dataList：被选择的数据（支持多行）
	 * 
	 * @return
	 * @throws Exception
	 */
	@POST
	@Path("fly")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public Response callScript(Map<String, Object> data) throws Exception{
		
		try {
			
			long formId = MapHelper.getLong(data, "formId", -1);
			long btnId = MapHelper.getLong(data, "btnId", -1);
			List<Map<String, Object>> dataList = (List<Map<String, Object>>) MapHelper.get(data, "dataList");
			
			Object result = DataBtnServiceImpl.DataServer(formId, btnId, dataList);
			
			if(result instanceof String) {
				
				return this.builder(200, null, (String)result);
				
			}else {
				return this.builder(200, result, "成功");
			}
			
		} catch(Throwable e){

			return builder(e, "/cop/data_btn/fly", null);
		}
	}
}
